package com.hzmct.demos;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.x6.gpioctl.GpioUtils;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author Woong on 2020/4/23
 * @website http://woong.cn
 */
public class GpioActivity extends AppCompatActivity {
    public static final String TAG = "GpioActivity";

    private Button btnOpen;
    private Button btnClose;
    private Button btnHigh;
    private Button btnLow;
    private Button btnRead;
    private EditText etGpioWrite;
    private EditText etGpioRead;

    private GpioUtils gpioUtils = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpio);

        btnOpen = findViewById(R.id.btn_open);
        btnClose = findViewById(R.id.btn_close);
        btnHigh = findViewById(R.id.btn_high);
        btnLow = findViewById(R.id.btn_low);
        btnRead = findViewById(R.id.btn_read);
        etGpioWrite = findViewById(R.id.et_gpio_write);
        etGpioRead = findViewById(R.id.et_gpio_read);

        btnRead.setText(String.format(getResources().getString(R.string.gpio_state), ""));

        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gpioUtils == null) {
                    //打开GPIO, 以RK芯片为例
                    gpioUtils = GpioUtils.getInstance("/dev/rk_gpio");
                }
            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gpioUtils != null) {
                    gpioUtils.close();
                    gpioUtils = null;
                }
            }
        });

        btnHigh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gpioUtils != null) {
                    try {
                        int gpioId = Integer.parseInt(etGpioWrite.getText().toString().trim());
                        gpioUtils.setGpioDirection(gpioId, GpioUtils.GPIO_DIRECTION_OUT);
                        gpioUtils.gpioSetValue(gpioId, GpioUtils.GPIO_VALUE_HIGH);
                    } catch (Exception e) {
                        Toast.makeText(GpioActivity.this, "请输入正确的GPIO口", Toast.LENGTH_SHORT);
                    }
                }
            }
        });

        btnLow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gpioUtils != null) {
                    try {
                        int gpioId = Integer.parseInt(etGpioWrite.getText().toString().trim());
                        gpioUtils.setGpioDirection(gpioId, GpioUtils.GPIO_DIRECTION_OUT);
                        gpioUtils.gpioSetValue(gpioId, GpioUtils.GPIO_VALUE_LOW);
                    } catch (Exception e) {
                        Toast.makeText(GpioActivity.this, "请输入正确的GPIO口", Toast.LENGTH_SHORT);
                    }
                }
            }
        });

        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gpioUtils != null) {
                    try {
                        int gpioId = Integer.parseInt(etGpioRead.getText().toString().trim());
                        gpioUtils.setGpioDirection(gpioId, GpioUtils.GPIO_DIRECTION_IN);
                        String gpioValue = gpioUtils.gpioGetValue(gpioId) == 0 ? "低" : "高";
                        btnRead.setText(String.format(getResources().getString(R.string.gpio_state), gpioValue));
                    } catch (Exception e) {
                        Toast.makeText(GpioActivity.this, "请输入正确的GPIO口", Toast.LENGTH_SHORT);
                    }
                }
            }
        });
    }
}
